<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail" >
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="幼儿" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentId">
              <a-select v-model="model.studentId" placeholder="请选择幼儿姓名"  mode="multiple"  @change="getClassId">
                <a-select-option v-for="item in studentList" :key="item.id" :value="item.id">{{ item.name }}</a-select-option>
              </a-select> 
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="评价类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="evaluationType">
              <a-select v-model="model.evaluationType" placeholder="请选择评价类型">
                <a-select-option v-for="item in evaluationList" :key="item.value" :value="item.value">{{ item.text }}</a-select-option>
              </a-select> 
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="formDisabled">
            <a-form-model-item label="评价种类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="evaluationType">
              <a-input v-model="model.evaluationCategory_dictText"   ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-tabs default-active-key="1" v-if="!formDisabled" v-model="model.type">
          <a-tab-pane key="1" tab="推送家长">
            <sk-evaluation ref="parent" :evaluationData="evaluationData" uploadId="parent" :evaluationCategory="1"></sk-evaluation>
          </a-tab-pane>
          <a-tab-pane key="0" tab="内部管理" forceRender>
            <sk-evaluation ref="school" :evaluationData="evaluationData" uploadId="school" :evaluationCategory="0"></sk-evaluation>
          </a-tab-pane>
        </a-tabs>
        <sk-evaluation  :evaluationDataInfo="evaluationDataInfo"  v-else :disabled="disabled"></sk-evaluation>
      </a-form-model>
    </j-form-container>
    <!-- 查看详情时显示 -->
    <a-form-model ref="form" :model="model" v-if="disabled">
      <a-row>
        <a-col :span="24" v-if="model.photo">
          <a-form-model-item label="照片" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <j-image-upload  v-model="model.photo" :disabled="disabled"></j-image-upload>
          </a-form-model-item>
        </a-col>
        <a-col :span="24" v-if="model.audio">
          <a-form-model-item label="语音评价" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <audio :src="model.audio | FileAccessHttpUrl" controls></audio>
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
  </a-spin>
</template>

<script>
import Vue from "vue";
import { httpAction, getAction,getFileAccessHttpUrl } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import {ajaxGetDictItems} from '@/api/api'
import { USER_INFO } from "@/store/mutation-types"
import SkEvaluation from "@/components/skComponent/skEvaluation.vue";
export default {
  name: 'SkGoodsForm',
  components: {
    SkEvaluation  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
        type:"1"
      },
      studentList:[],    //学生列表
      evaluationList:[],//评价类型
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        studentId: [{ required: true, message: '请选择幼儿姓名!' }],
        evaluationType: [{ required: true, message: '请选择评价类型!' }],
      },
      url: {
        add: '/management/skStudentEvaluation/add',
        edit: '/management/skStudentEvaluation/edit',
        queryById: '/management/skStudentEvaluation/queryById'
      },
      classId:[],
      evaluationDataInfo:null
    }
  },
  filters: {
    FileAccessHttpUrl(url){
      return getFileAccessHttpUrl(url)
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
    evaluationData:{
      get(){
        return {
          studentId:this.model.studentId,
          studentList:this.studentList,
          classId:this.classId,
          evaluationType:this.model.evaluationType,
          type:this.model.type
        }
      }
    }
  },
  async created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    // 获取评价类型
    getEvaluation(){
      //根据字典Code, 初始化字典数组
      ajaxGetDictItems("evaluation_type", null).then((res) => {
        if (res.success) {
          if (!this.model.id) {
            this.evaluationList =  res.result.filter(item=>item.value != 5 )
          }else{
            this.evaluationList =  res.result
          }
        }
      })
    },
    // 获取学生列表
    getStudentList(){
      const url = "/management/skStudent/getStudentList";
      const params = {
        classId:Vue.ls.get(USER_INFO).classId
      }
      getAction(url,params).then((res)=>{
        if(res.success){
          this.studentList = res.result;
          if (this.model.id) {
            this.queryInfo();
          }
        } else {
          this.$message.warning(res.message);
        }
      }).catch((error)=>{
        console.error(error);
      })
    },
    // 选择该学生所在的班级
    getClassId(e){
      this.classId = []
      e.forEach(el => {
        let studentItem = this.studentList.find(item=>item.id == el);
        this.classId.push({stuId:studentItem.id,classId:studentItem.classId});
      });
    },
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.getStudentList();
      this.getEvaluation();
      this.visible = true
    },
    queryInfo(){
      const params = {
        id:this.model.id
      }
      getAction(this.url.queryById,params).then((res)=>{
        if(res.success){
          this.classId.push(res.result.classId)
          this.model.evaluationType = String(res.result.evaluationType)
          this.model.audio=res.result.audio
          this.evaluationDataInfo = {
            studentId:res.result.studentId,
            studentList:this.studentList,
            classId:this.classId,
            evaluationContent:res.result.evaluationContent,
            photo:res.result.photo,
            evaluationCategory:res.result.evaluationCategory,
            studentDimensionEvaluationList:res.result.studentDimensionEvaluationList
          }
          // if (res.result.studentDimensionEvaluationList.length != 0 && (res.result.audio == null || res.result.audio == "")) {
					// 	this.evaluationDataInfo.evaluateModeNum = 0;
					// } else{
					// 	this.model.evaluateModeNum = 1;
					// }
        } else {
          this.$message.warning(res.message);
        }
      }).catch((error)=>{
        console.error(error);
      })
    },
    submitForm() {
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          this.submit();
        }
      })
    },
    async submit(){
      let parentData = await this.$refs.parent.onSubmit();
      let schoolData = await this.$refs.school.onSubmit();
      let data = parentData.concat(schoolData)
      if (data) {
        const that = this
        that.confirmLoading = true
        this.$emit('modelLoading', that.confirmLoading)
        let httpurl = ''
        let method = ''
        if (!this.model.id) {
          httpurl += this.url.add
          method = 'post'
        } else {
          httpurl += this.url.edit
          method = 'put'
        }
        httpAction(httpurl, data, method)
          .then(res => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          })
          .finally(() => {
            that.confirmLoading = false
            this.$emit('modelLoading', that.confirmLoading)
          })
      }
    }
  }
}
</script>
<style scoped>

</style>
